<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>gevent | 爬虫</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../进程线程协程/green_let.html" />
    
    
    <link rel="prev" href="../进程线程协程/introduce.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="7.1"
        data-chapter-title="gevent"
        data-filepath="进程线程协程/gevent.md"
        data-basepath=".."
        data-revision="Fri Oct 19 2018 08:44:08 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        序言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="认识爬虫/introduceSpider.html">
            
                
                    <a href="../认识爬虫/introduceSpider.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        认识爬虫
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="认识爬虫/http.html">
            
                
                    <a href="../认识爬虫/http.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        HTTP
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="认识爬虫/Requests.html">
            
                
                    <a href="../认识爬虫/Requests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        requests
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="认识爬虫/初步爬虫.html">
            
                
                    <a href="../认识爬虫/初步爬虫.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        初步爬虫小项目
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="正则表达式/正则表达式.html">
            
                
                    <a href="../正则表达式/正则表达式.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        正则表达式
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="正则表达式/正则表达式练习.html">
            
                
                    <a href="../正则表达式/正则表达式练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        正则表达式练习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="正则表达式/正则表达式之后的第一个项目.html">
            
                
                    <a href="../正则表达式/正则表达式之后的第一个项目.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        正则表达式之后的第一个项目
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="正则表达式/正则表达式常用表.html">
            
                
                    <a href="../正则表达式/正则表达式常用表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        正则表达式常用表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="正则表达式/正则表达式重点1.html">
            
                
                    <a href="../正则表达式/正则表达式重点1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        正则表达式重点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="正则表达式/正则表达式常问问题.html">
            
                
                    <a href="../正则表达式/正则表达式常问问题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        正则表达式常问问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="正则表达式/正则爬虫练习.html">
            
                
                    <a href="../正则表达式/正则爬虫练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        正则爬虫项目练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="XPATH/xpath-in.html">
            
                
                    <a href="../XPATH/xpath-in.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        XPATH
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="XPATH/Xpath.html">
            
                
                    <a href="../XPATH/Xpath.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        XPATH认识
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="XPATH/Xpath的代码例子.html">
            
                
                    <a href="../XPATH/Xpath的代码例子.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        XPATH的代码例子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="XPATH/xpath的爬虫练习.html">
            
                
                    <a href="../XPATH/xpath的爬虫练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        XPATH的爬虫练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="BeautifulSoup/BeautifulSoup.html">
            
                
                    <a href="../BeautifulSoup/BeautifulSoup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        BeautifulSoup
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="BeautifulSoup/常用的css选择器.html">
            
                
                    <a href="../BeautifulSoup/常用的css选择器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        常用的css选择器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="BeautifulSoup/BeautifulSoup4的各种例子.html">
            
                
                    <a href="../BeautifulSoup/BeautifulSoup4的各种例子.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        BeautifulSoup4的各种例子
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="代理/proxy.html">
            
                
                    <a href="../代理/proxy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        ip代理池项目
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="Selenium/Selenium.html">
            
                
                    <a href="../Selenium/Selenium.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Selenium
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="进程线程协程/introduce.html">
            
                
                    <a href="../进程线程协程/introduce.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        进程线程协程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="7.1" data-path="进程线程协程/gevent.html">
            
                
                    <a href="../进程线程协程/gevent.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        gevent
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="进程线程协程/green_let.html">
            
                
                    <a href="../进程线程协程/green_let.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        green_let
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="进程线程协程/yield.html">
            
                
                    <a href="../进程线程协程/yield.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        yield
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="进程线程协程/multiprecessing.html">
            
                
                    <a href="../进程线程协程/multiprecessing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        multiprecessing
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="进程线程协程/threading.html">
            
                
                    <a href="../进程线程协程/threading.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        threading
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="scrapy框架/scrapy.html">
            
                
                    <a href="../scrapy框架/scrapy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        Scrapy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="scrapy框架/scrapy_setting.html">
            
                
                    <a href="../scrapy框架/scrapy_setting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        scrapy_setting
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="scrapy框架/模块作用.html">
            
                
                    <a href="../scrapy框架/模块作用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        模块作用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="scrapy框架/19个中间件.html">
            
                
                    <a href="../scrapy框架/19个中间件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        19个中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="scrapy-redis分布式/scrapy-redis.html">
            
                
                    <a href="../scrapy-redis分布式/scrapy-redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        scrapy-redis分布式
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="scrapy-redis分布式/scrapy-redis的改造方法.html">
            
                
                    <a href="../scrapy-redis分布式/scrapy-redis的改造方法.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        scrapy-redis的改造方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="spider_projects/introduce.html">
            
                
                    <a href="../spider_projects/introduce.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        spider_projects
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="spider_projects/使用代理在普通爬虫脚本下.html">
            
                
                    <a href="../spider_projects/使用代理在普通爬虫脚本下.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        使用代理在普通爬虫脚本下
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="spider_projects/xpath爬取前程无忧的数据.html">
            
                
                    <a href="../spider_projects/xpath爬取前程无忧的数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        xpath爬取前程无忧的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="spider_projects/存储数据库的脚本.html">
            
                
                    <a href="../spider_projects/存储数据库的脚本.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        存储数据库的脚本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="spider_projects/weibo.html">
            
                
                    <a href="../spider_projects/weibo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        爬取央视新闻微博
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="spider_projects/weibo2.html">
            
                
                    <a href="../spider_projects/weibo2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        爬取明星的微博
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="spider_projects/liepin.html">
            
                
                    <a href="../spider_projects/liepin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        爬取猎聘的职位信息Crawl
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="spider_projects/zhilianRe.html">
            
                
                    <a href="../spider_projects/zhilianRe.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        纯正则爬取智联的职位信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="spider_projects/setting.html">
            
                
                    <a href="../spider_projects/setting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        scrapy中的setting
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="spider_projects/items.html">
            
                
                    <a href="../spider_projects/items.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        scrapy中的items
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="spider_projects/pipeline.html">
            
                
                    <a href="../spider_projects/pipeline.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        scrapy中的pipeline
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.11" data-path="spider_projects/middlewares.html">
            
                
                    <a href="../spider_projects/middlewares.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.11.</b>
                        
                        scrapy中的middlewares
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.12" data-path="spider_projects/框架中的代码运行脚本.html">
            
                
                    <a href="../spider_projects/框架中的代码运行脚本.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.12.</b>
                        
                        框架中的代码运行脚本
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="爬虫的面试题/面试题1-10.html">
            
                
                    <a href="../爬虫的面试题/面试题1-10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        爬虫面试题
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >爬虫</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <hr>
<h1 id="gevent">Gevent</h1>
<p>Gevent&#x662F;&#x4E00;&#x79CD;&#x57FA;&#x4E8E;&#x534F;&#x7A0B;&#x7684;Python&#x7F51;&#x7EDC;&#x5E93;&#xFF0C;&#x5B83;&#x7528;&#x5230;Greenlet&#x63D0;&#x4F9B;&#x7684;&#xFF0C;&#x5C01;&#x88C5;&#x4E86;libevent&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x7684;&#x9AD8;&#x5C42;&#x540C;&#x6B65;API&#x3002;&#x5B83;&#x8BA9;&#x5F00;&#x53D1;&#x8005;&#x5728;&#x4E0D;&#x6539;&#x53D8;&#x7F16;&#x7A0B;&#x4E60;&#x60EF;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x7528;&#x540C;&#x6B65;&#x7684;&#x65B9;&#x5F0F;&#x5199;&#x5F02;&#x6B65;I/O&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x4F7F;&#x7528;Gevent&#x7684;&#x6027;&#x80FD;&#x786E;&#x5B9E;&#x8981;&#x6BD4;&#x7528;&#x4F20;&#x7EDF;&#x7684;&#x7EBF;&#x7A0B;&#x9AD8;&#xFF0C;&#x751A;&#x81F3;&#x9AD8;&#x5F88;&#x591A;&#x3002;&#x4F46;&#x8FD9;&#x91CC;&#x4E0D;&#x5F97;&#x4E0D;&#x8BF4;&#x5B83;&#x7684;&#x4E00;&#x4E2A;&#x5751;&#xFF1A;</p>
<ul>
<li><p>Monkey-patching&#xFF0C;&#x6211;&#x4EEC;&#x90FD;&#x53EB;&#x7334;&#x5B50;&#x8865;&#x4E01;&#xFF0C;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x4E86;&#x8FD9;&#x4E2A;&#x8865;&#x4E01;&#xFF0C;Gevent&#x76F4;&#x63A5;&#x4FEE;&#x6539;&#x6807;&#x51C6;&#x5E93;&#x91CC;&#x9762;&#x5927;&#x90E8;&#x5206;&#x7684;&#x963B;&#x585E;&#x5F0F;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#xFF0C;&#x5305;&#x62EC;socket&#x3001;ssl&#x3001;threading&#x548C; select&#x7B49;&#x6A21;&#x5757;&#xFF0C;&#x800C;&#x53D8;&#x4E3A;&#x534F;&#x4F5C;&#x5F0F;&#x8FD0;&#x884C;&#x3002;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x65E0;&#x6CD5;&#x4FDD;&#x8BC1;&#x4F60;&#x5728;&#x590D;&#x6742;&#x7684;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#x4E2D;&#x6709;&#x54EA;&#x4E9B;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x6807;&#x51C6;&#x5E93;&#x4F1A;&#x7531;&#x4E8E;&#x6253;&#x4E86;&#x8865;&#x4E01;&#x800C;&#x51FA;&#x73B0;&#x5947;&#x602A;&#x7684;&#x95EE;&#x9898;</p>
</li>
<li><p>&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#x652F;&#x6301;&#x3002;&#x5F97;&#x786E;&#x4FDD;&#x9879;&#x76EE;&#x4E2D;&#x7528;&#x5230;&#x5176;&#x4ED6;&#x7528;&#x5230;&#x7684;&#x7F51;&#x7EDC;&#x5E93;&#x4E5F;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x7EAF;Python&#x6216;&#x8005;&#x660E;&#x786E;&#x8BF4;&#x660E;&#x652F;&#x6301;Gevent</p>
</li>
</ul>
<hr>
<p>&#x65E2;&#x7136;Gevent&#x7528;&#x7684;&#x662F;Greenlet&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E0B;&#x56FE;&#x6765;&#x7406;&#x89E3;greenlet:</p>
<p><img src="images/gevent.png" alt="gevent"></p>
<p>&#x6BCF;&#x4E2A;&#x534F;&#x7A0B;&#x90FD;&#x6709;&#x4E00;&#x4E2A;parent,&#x6700;&#x9876;&#x5C42;&#x7684;&#x534F;&#x7A0B;&#x5C31;&#x662F;man thread&#x6216;&#x8005;&#x662F;&#x5F53;&#x524D;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x6BCF;&#x4E2A;&#x534F;&#x7A0B;&#x9047;&#x5230;IO&#x7684;&#x65F6;&#x5019;&#x5C31;&#x628A;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x7ED9;&#x6700;&#x9876;&#x5C42;&#x7684;&#x534F;&#x7A0B;&#xFF0C;&#x5B83;&#x4F1A;&#x770B;&#x90A3;&#x4E2A;&#x534F;&#x7A0B;&#x7684;IO event&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#xFF0C;&#x5C31;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x7ED9;&#x5B83;&#x3002;</p>
<hr>
<h2 id="&#x4E0B;&#x9762;&#x662F;gevent&#x7684;&#x4F8B;&#x5B50;&#xFF1A;">&#x4E0B;&#x9762;&#x662F;gevent&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> gevent

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func1</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&quot;func1 running&quot;</span>)
    gevent.sleep(<span class="hljs-number">2</span>)             <span class="hljs-comment"># &#x5185;&#x90E8;&#x51FD;&#x6570;&#x5B9E;&#x73B0;io&#x64CD;&#x4F5C;</span>
    print(<span class="hljs-string">&quot;switch func1&quot;</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func2</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&quot;func2 running&quot;</span>)
    gevent.sleep(<span class="hljs-number">1</span>)
    print(<span class="hljs-string">&quot;switch func2&quot;</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func3</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&quot;func3  running&quot;</span>)
    gevent.sleep(<span class="hljs-number">0</span>)
    print(<span class="hljs-string">&quot;func3 done..&quot;</span>)

gevent.joinall([gevent.spawn(func1),
                gevent.spawn(func2),
                gevent.spawn(func3),
                ])
</code></pre>
<p>&#x89E3;&#x91CA;&#x4E0B;&#xFF1A;</p>
<ul>
<li><p>&#x201D;gevent.spawn()&#x201D;&#x65B9;&#x6CD5;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;greenlet&#x534F;&#x7A0B;&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x8FD0;&#x884C;&#x5B83;&#x3002;</p>
</li>
<li><p>&#x201D;gevent.joinall()&#x201D;&#x65B9;&#x6CD5;&#x4F1A;&#x7B49;&#x5F85;&#x6240;&#x6709;&#x4F20;&#x5165;&#x7684;greenlet&#x534F;&#x7A0B;&#x8FD0;&#x884C;&#x7ED3;&#x675F;&#x540E;&#x518D;&#x9000;&#x51FA;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x201D;timeout&#x201D;&#x53C2;&#x6570;&#x6765;&#x8BBE;&#x7F6E;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x5355;&#x4F4D;&#x662F;&#x79D2;&#x3002;</p>
</li>
</ul>
<h3 id="&#x8FD0;&#x884C;&#x7684;&#x7ED3;&#x679C;&#xFF1A;">&#x8FD0;&#x884C;&#x7684;&#x7ED3;&#x679C;&#xFF1A;</h3>
<pre><code class="lang-python">func1 running
func2 running
func3  running
func3 done..
switch func2
switch func1
</code></pre>
<p>&#x9996;&#x5148;&#x8FDB;&#x5165;&#x534F;&#x7A0B;func1&#xFF0C;&#x6253;&#x5370;func1&#x6267;&#x884C;&#x7B2C;&#x4E00;&#x4E2A;&#x6253;&#x5370; func1 running</p>
<p>&#x9047;&#x5230;io&#x963B;&#x585E;&#x9047;&#x5230; sleep2 &#x64CD;&#x4F5C;&#xFF0C;sleep&#x4EE5;&#x540E;&#x90A3;&#x4E48;&#x5C31;&#x6267;&#x884C;func2&#xFF0C;&#x6253;&#x5370;func2&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x6253;&#x5370;&#xFF1A;func2 running</p>
<p>&#x9047;&#x5230;sleep1 &#x7136;&#x540E;&#x6267;&#x884C; func3  &#x6267;&#x884C;&#x7B2C;&#x4E00;&#x4E2A;func3 &#x7684;&#x6253;&#x5370;&#xFF1A; func3 running&#x3002; sleep 0 &#x4E4B;&#x540E;&#x6253;&#x5370; func3 done&#x3002;</p>
<p>&#x90A3;&#x4E48;&#x6B64;&#x65F6;sleep 1s &#x7ED3;&#x675F;&#xFF0C;&#x5207;&#x6362;&#x56DE;func2&#xFF0C;&#x6253;&#x5370; switch func2&#xFF0C;&#x6B64;&#x65F6;sleep 2 &#x7ED3;&#x675F;&#xFF0C;&#x5207;&#x6362;&#x56DE;func1&#xFF0C;&#x6253;&#x5370; switch func1.</p>
<hr>
<h2 id="&#x7C7B;&#x4F3C;&#x7684;&#x4E00;&#x4E2A;&#x89E3;&#x91CA;&#x8FC7;&#x7A0B;">&#x7C7B;&#x4F3C;&#x7684;&#x4E00;&#x4E2A;&#x89E3;&#x91CA;&#x8FC7;&#x7A0B;</h2>
<p><img src="images/flow.gif" alt="gevent"></p>
<hr>
<h1 id="&#x9501;&#x548C;&#x4FE1;&#x53F7;&#x91CF;">&#x9501;&#x548C;&#x4FE1;&#x53F7;&#x91CF;</h1>
<p>&#x4FE1;&#x53F7;&#x91CF;&#x662F;&#x4E00;&#x4E2A;&#x5141;&#x8BB8;greenlet&#x76F8;&#x4E92;&#x5408;&#x4F5C;&#xFF0C;&#x9650;&#x5236;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x6216;&#x8FD0;&#x884C;&#x7684;&#x4F4E;&#x5C42;&#x6B21;&#x7684;&#x540C;&#x6B65;&#x539F;&#x8BED;&#x3002; &#x4FE1;&#x53F7;&#x91CF;&#x6709;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;acquire&#x548C;release&#x3002;&#x5728;&#x4FE1;&#x53F7;&#x91CF;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x88AB; acquire&#x6216;release&#xFF0C;&#x548C;&#x62E5;&#x6709;&#x8D44;&#x6E90;&#x7684;&#x6570;&#x91CF;&#x4E4B;&#x95F4;&#x4E0D;&#x540C;&#xFF0C;&#x88AB;&#x79F0;&#x4E3A;&#x6B64;&#x4FE1;&#x53F7;&#x91CF;&#x7684;&#x8303;&#x56F4; (the bound of the semaphore)&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x4FE1;&#x53F7;&#x91CF;&#x7684;&#x8303;&#x56F4;&#x5DF2;&#x7ECF;&#x964D;&#x4F4E;&#x5230;0&#xFF0C;&#x5B83;&#x4F1A; &#x963B;&#x585E;acquire&#x64CD;&#x4F5C;&#x76F4;&#x5230;&#x53E6;&#x4E00;&#x4E2A;&#x5DF2;&#x7ECF;&#x83B7;&#x5F97;&#x4FE1;&#x53F7;&#x91CF;&#x7684;greenlet&#x4F5C;&#x51FA;&#x91CA;&#x653E;&#x3002;</p>
<hr>
<h1 id="&#x7334;&#x5B50;&#x8865;&#x4E01;monkey-patching">&#x7334;&#x5B50;&#x8865;&#x4E01;(Monkey patching)</h1>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x6765;&#x5230;gevent&#x7684;&#x6B7B;&#x89D2;&#x4E86;. &#x5728;&#x6B64;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x5DF2;&#x7ECF;&#x907F;&#x514D;&#x63D0;&#x5230;&#x7334;&#x5B50;&#x8865;&#x4E01;(monkey patching) &#x4EE5;&#x5C1D;&#x8BD5;&#x4F7F;gevent&#x8FD9;&#x4E2A;&#x5F3A;&#x5927;&#x7684;&#x534F;&#x7A0B;&#x6A21;&#x578B;&#x53D8;&#x5F97;&#x751F;&#x52A8;&#x6709;&#x8DA3;&#xFF0C;&#x4F46;&#x73B0;&#x5728;&#x5230;&#x4E86;&#x8BA8;&#x8BBA;&#x7334;&#x5B50;&#x8865;&#x4E01;&#x7684;&#x9ED1;&#x8272;&#x827A;&#x672F; &#x7684;&#x65F6;&#x5019;&#x4E86;&#x3002;</p>
<p>&#x4F60;&#x4E4B;&#x524D;&#x53EF;&#x80FD;&#x6CE8;&#x610F;&#x5230;&#x6211;&#x4EEC;&#x63D0;&#x5230;&#x4E86;monkey.patch_socket()&#x8FD9;&#x4E2A;&#x547D;&#x4EE4;&#xFF0C;&#x8FD9;&#x4E2A; &#x7EAF;&#x7CB9;&#x526F;&#x4F5C;&#x7528;&#x547D;&#x4EE4;&#x662F;&#x7528;&#x6765;&#x6539;&#x53D8;&#x6807;&#x51C6;socket&#x5E93;&#x7684;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> socket
print(socket.socket)

print(<span class="hljs-string">&quot;After monkey patch&quot;</span>)

<span class="hljs-keyword">from</span> gevent <span class="hljs-keyword">import</span> monkey

monkey.patch_socket()
print(socket.socket)

<span class="hljs-keyword">import</span> select

print(select.select)
monkey.patch_select()
print(<span class="hljs-string">&quot;After monkey patch&quot;</span>)
print(select.select)
</code></pre>
<h3 id="&#x6267;&#x884C;&#x7ED3;&#x679C;">&#x6267;&#x884C;&#x7ED3;&#x679C;</h3>
<pre><code class="lang-python">
&lt;class &apos;socket.socket&apos;&gt;

After monkey patch

&lt;class &apos;gevent._socket3.socket&apos;&gt;

&lt;built-in function select&gt;

After monkey patch

&lt;function select at 0x0000000004582AE8&gt;
</code></pre>
<hr>
<h2 id="&#x4F8B;&#x5B50;&#xFF1A;">&#x4F8B;&#x5B50;&#xFF1A;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> gevent
<span class="hljs-keyword">import</span> socket

urls = [<span class="hljs-string">&apos;www.baidu.com&apos;</span>, <span class="hljs-string">&apos;www.gevent.org&apos;</span>, <span class="hljs-string">&apos;www.python.org&apos;</span>]
jobs = [gevent.spawn(socket.gethostbyname, url) <span class="hljs-keyword">for</span> url <span class="hljs-keyword">in</span> urls]
gevent.joinall(jobs, timeout=<span class="hljs-number">5</span>)

<span class="hljs-keyword">print</span> [job.value <span class="hljs-keyword">for</span> job <span class="hljs-keyword">in</span> jobs]
</code></pre>
<p>&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x534F;&#x7A0B;&#x5206;&#x522B;&#x83B7;&#x53D6;&#x4E09;&#x4E2A;&#x7F51;&#x7AD9;&#x7684;IP&#x5730;&#x5740;&#xFF0C;&#x7531;&#x4E8E;&#x6253;&#x5F00;&#x8FDC;&#x7A0B;&#x5730;&#x5740;&#x4F1A;&#x5F15;&#x8D77;IO&#x963B;&#x585E;&#xFF0C;&#x6240;&#x4EE5;gevent&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x5EA6;&#x4E0D;&#x540C;&#x7684;&#x534F;&#x7A0B;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x534F;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;&#x201D;value&#x201D;&#x5C5E;&#x6027;&#xFF0C;&#x6765;&#x83B7;&#x53D6;&#x534F;&#x7A0B;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<hr>
<p>&#xFF0C;&#x5176;&#x5B9E;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x95F4;&#x540C;&#x4E0D;&#x7528;&#x534F;&#x7A0B;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x662F;&#x4E09;&#x4E2A;&#x7F51;&#x7AD9;&#x6253;&#x5F00;&#x65F6;&#x95F4;&#x7684;&#x603B;&#x548C;&#x3002;&#x53EF;&#x662F;&#x7406;&#x8BBA;&#x4E0A;&#x534F;&#x7A0B;&#x662F;&#x975E;&#x963B;&#x585E;&#x7684;&#xFF0C;&#x90A3;&#x8FD0;&#x884C;&#x65F6;&#x95F4;&#x5E94;&#x8BE5;&#x7B49;&#x4E8E;&#x6700;&#x957F;&#x7684;&#x90A3;&#x4E2A;&#x7F51;&#x7AD9;&#x6253;&#x5F00;&#x65F6;&#x95F4;&#x5440;&#xFF1F;</p>
<p>&#xFF1F;&#x5176;&#x5B9E;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;Python&#x6807;&#x51C6;&#x5E93;&#x91CC;&#x7684;socket&#x662F;&#x963B;&#x585E;&#x5F0F;&#x7684;&#xFF0C;DNS&#x89E3;&#x6790;&#x65E0;&#x6CD5;&#x5E76;&#x53D1;&#xFF0C;&#x5305;&#x62EC;&#x50CF;urllib&#x5E93;&#x4E5F;&#x4E00;&#x6837;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x7528;&#x534F;&#x7A0B;&#x5B8C;&#x5168;&#x6CA1;&#x610F;&#x4E49;&#x3002;&#x90A3;&#x600E;&#x4E48;&#x529E;&#xFF1F;</p>
<p>&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528;gevent&#x4E0B;&#x7684;socket&#x6A21;&#x5757;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x201D;from gevent import socket&#x201D;&#x6765;&#x5BFC;&#x5165;&#x3002;&#x4E0D;&#x8FC7;&#x66F4;&#x5E38;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528;&#x7334;&#x5B50;&#x5E03;&#x4E01;&#xFF08;Monkey patching&#xFF09;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> gevent <span class="hljs-keyword">import</span> monkey; monkey.patch_socket()
<span class="hljs-keyword">import</span> gevent
<span class="hljs-keyword">import</span> socket

urls = [<span class="hljs-string">&apos;www.baidu.com&apos;</span>, <span class="hljs-string">&apos;www.gevent.org&apos;</span>, <span class="hljs-string">&apos;www.python.org&apos;</span>]
jobs = [gevent.spawn(socket.gethostbyname, url) <span class="hljs-keyword">for</span> url <span class="hljs-keyword">in</span> urls]
gevent.joinall(jobs, timeout=<span class="hljs-number">5</span>)

<span class="hljs-keyword">print</span> [job.value <span class="hljs-keyword">for</span> job <span class="hljs-keyword">in</span> jobs]
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x7684;&#x7B2C;&#x4E00;&#x884C;&#x5C31;&#x662F;&#x5BF9;socket&#x6807;&#x51C6;&#x5E93;&#x6253;&#x4E0A;&#x7334;&#x5B50;&#x8865;&#x4E01;&#xFF0C;&#x6B64;&#x540E;socket&#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684;&#x7C7B;&#x548C;&#x65B9;&#x6CD5;&#x90FD;&#x4F1A;&#x88AB;&#x66FF;&#x6362;&#x6210;&#x975E;&#x963B;&#x585E;&#x5F0F;&#x7684;&#xFF0C;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x7684;&#x4EE3;&#x7801;&#x90FD;&#x4E0D;&#x7528;&#x4FEE;&#x6539;&#xFF0C;&#x8FD9;&#x6837;&#x534F;&#x7A0B;&#x7684;&#x6548;&#x7387;&#x5C31;&#x771F;&#x6B63;&#x4F53;&#x73B0;&#x51FA;&#x6765;&#x4E86;&#x3002;Python&#x4E2D;&#x5176;&#x5B83;&#x6807;&#x51C6;&#x5E93;&#x4E5F;&#x5B58;&#x5728;&#x963B;&#x585E;&#x7684;&#x60C5;&#x51B5;&#xFF0C;gevent&#x63D0;&#x4F9B;&#x4E86;&#x201D;monkey.patch_all()&#x201D;&#x65B9;&#x6CD5;&#x5C06;&#x6240;&#x6709;&#x6807;&#x51C6;&#x5E93;&#x90FD;&#x66FF;&#x6362;.</p>
<pre><code>from gevent import monkey; monkey.patch_all()
</code></pre><p>&#x4F7F;&#x7528;&#x7334;&#x5B50;&#x8865;&#x4E01;&#x8912;&#x8D2C;&#x4E0D;&#x4E00;&#xFF0C;&#x4F46;&#x662F;&#x5B98;&#x7F51;&#x4E0A;&#x8FD8;&#x662F;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x201D;patch_all()&#x201D;&#xFF0C;&#x800C;&#x4E14;&#x5728;&#x7A0B;&#x5E8F;&#x7684;&#x7B2C;&#x4E00;&#x884C;&#x5C31;&#x6267;&#x884C;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../进程线程协程/introduce.html" class="navigation navigation-prev " aria-label="Previous page: 进程线程协程"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../进程线程协程/green_let.html" class="navigation navigation-next " aria-label="Next page: green_let"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
